package org.subject.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.subject.dao.UserMapper;
import org.subject.entity.User;
import org.subject.service.UserService;

import java.util.List;

/**
 * 用户相关信息操作
 * Created by kai on 2017/5/1.
 */
@Service
public class UserServiceImpl implements UserService {
    Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    UserMapper userMapper;

    /**
     * 添加用户
     *
     * @param userName
     * @param password
     * @return
     */
    public boolean addUser(String userName, String password) {
        User user = new User();
        user.setUserNumber(userName);
        user.setUserName(userName);
        user.setUserPassword(password);
        int code = userMapper.insert(user);
        if (code > 0) {
            return true;
        }
        return false;
    }

    /**
     * 查询用户
     *
     * @return
     */
    public List<User> queryAllUser() {
        return userMapper.queryAllUser();
    }

    /**
     * 根据用户id，查询用户详细信息
     *
     * @param userId
     * @return
     */
    public User selectByPrimaryKey(String userId) {
        long id = 0;
        if (userId != null) {
            id = Long.parseLong(userId);
        }
        return userMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据用户id，更新用户信息
     *
     * @param user
     * @return
     */
    public int updateByPrimaryKey(User user) {
        return userMapper.updateByPrimaryKeySelective(user);
    }

    /**
     * 创建新用户
     *
     * @param user
     * @return
     */
    public int insertSelective(User user) {
        return userMapper.insert(user);
    }
}
